package com.ebt.m.policy.view;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.BaseListView;
import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.widgets.view.EBTDialog;
import com.ebt.m.policy.presenter.SearchHistoryPresenter;

/**
 * @author Damon.Han
 */
public class SearchHistoryListview extends BaseListView<SearchHistoryPresenter> {

    public static final int TAG_VIEW_YTPE = 0;
    public static final int HISTORY_VIEW_TYPE = 1;

    private int mFrom;

    public SearchHistoryListview(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchHistoryListview(Context context) {
        this(context, null);
    }

    public SearchHistoryListview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mRefreshLayout.setRefreshNeeded(false);
    }

    public IView.OnClickListner onClickListner = new IView.OnClickListner() {
        @Override
        public void onClick(View view, Object... args) {
            switch (view.getId()) {
                case R.id.delete:
                    deleteHistorys();
                    break;
            }
        }

        @Override
        public void onLongClick(View view, Object... args) {

        }
    };

    private void deleteHistorys() {
        EBTDialog ebtDialog = new EBTDialog();
        ebtDialog.setMessage("确定清空搜索记录吗？");
        ebtDialog.setRightButton("清空", v -> mPresenter.deleteHistory());
        ebtDialog.setLeftButton("取消", v -> ebtDialog.dismiss());
        ebtDialog.show(((AppCompatActivity) getContext()).getSupportFragmentManager(), getClass().getSimpleName());
    }

    @Override
    protected ISectionFactory createViewFactory() {
        return viewType -> {
            if (viewType == TAG_VIEW_YTPE) {
                SearchHistoryTitle searchHistoryTag = new SearchHistoryTitle(getContext());
                searchHistoryTag.setOnClickViewListener(onClickListner);
                return searchHistoryTag;
            } else if (viewType == HISTORY_VIEW_TYPE) {
                SearchHistoryContent lineBreakLayout = new SearchHistoryContent(getContext());
                lineBreakLayout.setOnClickViewListener(onClickListner);
                return lineBreakLayout;
            }
            return null;
        };
    }

    @Override
    protected SearchHistoryPresenter createPresenter() {
        return new SearchHistoryPresenter(getContext(), this);
    }

    @Override
    public void loadNext() {

    }

    @Override
    public void update(Object... args) {
        super.update(args);
        mFrom = (int) args[0];
        mPresenter.loadNew(args);
    }

}
